import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter_app/config/service_url.dart';

//第一个参数：url 是service_url中的请求网址的名称
//第二个参数：formData 是给service_url中的请求网址加上参数的
//也可以加上第三个参数是可以选择为 post 还是 get 方式请求
Future request(url , formData , method) async{
  try{
    Response response;
    Dio dio = Dio();
    if(formData==null){
      if(method == "get"){
        response = await dio.get(servicePath[url]);
      }else{
        response = await dio.post(servicePath[url]);
      }
    }else{
      if(method == "get") {
        response = await dio.get(servicePath[url], queryParameters: formData);
      }else{
        response = await dio.post(servicePath[url], queryParameters: formData);
      }
    }
    if (response.statusCode == 200){
      return response.data;
    }else{
      throw Exception("后端检测端口出现异常，请检测代码和服务器情况......");
    }
  }catch(e){
    return print("ERROR : ===========>$e");
  }
}